﻿package com.sxhalo.manage.web.action;

import java.util.*;

import org.apache.struts2.ServletActionContext;
import org.springframework.beans.BeanUtils;

import com.opensymphony.xwork2.ActionSupport;
import com.sxhalo.manage.map.SysModules;
import com.sxhalo.manage.map.SysOperation;
import com.sxhalo.manage.map.SysRoleModules;
import com.sxhalo.manage.map.SysRoleModulesId;
import com.sxhalo.manage.map.SysRoles;
import com.sxhalo.manage.service.IBaseService;
import com.sxhalo.manage.utils.Pager;
import com.sxhalo.manage.web.form.ModulesForm;
import com.sxhalo.manage.web.form.OperationForm;

public class ModulesAction extends ActionSupport {

	private static final long serialVersionUID = 1L;

	private int draw = 1;

	private int start = 0;

	private int length = 20;

	private int roleId;

	private ModulesForm modules;

	// 将会被Struts2序列化为JSON字符串的对象
	private Map<String, Object> dataMap;

	private List<ModulesForm> dataList;

	// 业务层
	private IBaseService<SysModules> myService;

	private IBaseService<SysRoleModules> roleService;

	public int getDraw() {
		return draw;
	}

	public void setDraw(int draw) {
		this.draw = draw;
	}

	public int getStart() {
		return start;
	}

	public void setStart(int start) {
		this.start = start;
	}

	public int getLength() {
		return length;
	}

	public void setLength(int length) {
		this.length = length;
	}

	public int getRoleId() {
		return roleId;
	}

	public void setRoleId(int roleId) {
		this.roleId = roleId;
	}

	public ModulesForm getModules() {
		return modules;
	}

	public void setModules(ModulesForm modules) {
		this.modules = modules;
	}

	public Map<String, Object> getDataMap() {
		return dataMap;
	}

	public void setDataMap(Map<String, Object> dataMap) {
		this.dataMap = dataMap;
	}

	public List<ModulesForm> getDataList() {
		return dataList;
	}

	public void setDataList(List<ModulesForm> dataList) {
		this.dataList = dataList;
	}

	public IBaseService<SysModules> getMyService() {
		return myService;
	}

	public void setMyService(IBaseService<SysModules> myService) {
		this.myService = myService;
	}

	public IBaseService<SysRoleModules> getRoleService() {
		return roleService;
	}

	public void setRoleService(IBaseService<SysRoleModules> roleService) {
		this.roleService = roleService;
	}

	/**
	 * 构造方法
	 */
	public ModulesAction() {
		// 初始化Map对象
		dataMap = new HashMap<String, Object>();
	}

	public String getEntity() {
		try {
			SysModules entity = myService.findById(modules.getModuleId());
			BeanUtils.copyProperties(entity, modules);
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String getGridData() {
		// dataMap中的数据将会被Struts2转换成JSON字符串，所以这里要先清空其中的数据
		dataMap.clear();
		// 获取数据
		try {
			int page = start == 0 ? 1 : start / length + 1;
			int size = length < 0 ? Integer.MAX_VALUE : length;
			Pager<SysModules> list = myService
					.findPagerList(page, size, "", "");
			dataMap.put("draw", draw);
			dataMap.put("recordsTotal", list.getTotal());
			dataMap.put("recordsFiltered", list.getTotal());
			dataMap.put("data", list.getList());
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String getAllData() {
		// 获取数据
		try {
			List<ModulesForm> data = new ArrayList<ModulesForm>();
			List<SysModules> list = myService.findAll();
			for (SysModules po : list) {
				ModulesForm vo = new ModulesForm();
				BeanUtils.copyProperties(po, vo);
				data.add(vo);
			}
			dataList = data;
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String getRoleModules() {
		// 获取数据
		try {
			SysRoles role = new SysRoles();
			role.setRoleId(roleId);
			List<SysRoleModules> roleModule = roleService.findList("sysRoles",
					role);
			List<ModulesForm> data = new ArrayList<ModulesForm>();
			List<SysModules> list = myService.findAll();
			for (SysModules po : list) {
				ModulesForm vo = new ModulesForm();
				BeanUtils.copyProperties(po, vo);
				List<OperationForm> operation = new ArrayList<OperationForm>();
				for (SysRoleModules rModule : roleModule) {
					if (rModule.getSysModules().getModuleId() == po
							.getModuleId()) {
						OperationForm e = new OperationForm();
						e.setOperationId(rModule.getSysOperation()
								.getOperationId());
						operation.add(e);
					}
				}
				vo.setOperation(operation);
				data.add(vo);
			}
			dataList = data;
		} catch (Exception re) {

		}
		// 返回结果
		return SUCCESS;
	}

	public String addEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysModules entity = new SysModules();
		BeanUtils.copyProperties(modules, entity);
		try {
			myService.save(entity);
			dataMap.put("data", true);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String editEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		SysModules entity = myService.findById(modules.getModuleId());
		entity.setModuleName(modules.getModuleName());
		entity.setModuleCode(modules.getModuleCode());
		entity.setModulePath(modules.getModulePath());
		entity.setModuleClass(modules.getModuleClass());
		entity.setModuleParent(modules.getModuleParent());
		entity.setModuleApp(modules.getModuleApp());
		entity.setModuleDescription(modules.getModuleDescription());
		entity.setModuleSort(modules.getModuleSort());
		entity.setModuleIsdisable(modules.getModuleIsdisable());
		try {
			myService.update(entity);
			dataMap.put("data", true);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	public String delEntity() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			SysModules entity = myService.findById(modules.getModuleId());
			if (myService.delete(entity)) {
				dataMap.put("valid", true);
			} else {
				dataMap.put("valid", false);
			}
		} catch (Exception re) {
			dataMap.put("valid", false);
		}
		// 返回结果
		return SUCCESS;
	}

	public String addOperation() {
		// 清空dataMap中的数据
		dataMap.clear();
		try {
			// 清除权限
			SysRoles role = new SysRoles();
			role.setRoleId(roleId);
			clearRoleOperation(role);
			for (ModulesForm vo : dataList) {
				SysModules module = new SysModules();
				module.setModuleId(vo.getModuleId());
				String ids = vo.getOperationIds();
				String[] arr = ids.equals("") ? new String[0] : ids.split(",");
				for (String id : arr) {
					SysOperation operation = new SysOperation();
					operation.setOperationId(Integer.parseInt(id));
					SysRoleModulesId mid = new SysRoleModulesId();
					mid.setSysRoles(role);
					mid.setSysModules(module);
					mid.setSysOperation(operation);
					SysRoleModules entity = new SysRoleModules();
					entity.setId(mid);
					roleService.save(entity);
				}
			}
			dataMap.put("data", true);
		} catch (Exception re) {
			dataMap.put("data", re.getMessage());
		}
		// 返回结果
		return SUCCESS;
	}

	private void clearRoleOperation(SysRoles role) {
		List<SysRoleModules> roleModule = roleService
				.findList("sysRoles", role);
		for (SysRoleModules ro : roleModule) {
			roleService.delete(ro);
		}
	}
}
